package gui;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;

import com.toedter.calendar.JDateChooser;

import lu.tudor.santec.jtimechooser.JTimeChooser;

import javax.swing.JComboBox;
import javax.swing.JButton;

import negocio.Clase;
import negocio.ClaseControlador;
import negocio.Cursada;
import negocio.Sala;
import negocio.SalaControlador;

import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.sql.Date;
import java.sql.Time;
import java.util.ArrayList;
import java.awt.Color;
import javax.swing.SwingConstants;

public class AgregarClase extends JFrame {

	private JPanel contentPane;
	Clase clase;
	Clases padreClases;
	Cursada cursada;
	ArrayList<Sala> salas;
	JComboBox comboBox;
	JLabel lblError;
	Salas verSalas;
	
	public void llenarComboSalas()
	{
		salas=SalaControlador.getAll();
		for(Sala s:salas)
		{
			comboBox.addItem(s.getNumero());
		}
		comboBox.setSelectedIndex(-1);
	}

	
	public AgregarClase(Clases padreClases, Cursada cursada) {
		addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent e){
				padreClases.nullAgregarClase();
				dispose();
			}
		});
		this.padreClases=padreClases;
		this.cursada=cursada;
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setBounds(100, 100, 245, 249);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		JLabel lblFecha = new JLabel("Fecha");
		lblFecha.setBounds(10, 17, 46, 14);
		contentPane.add(lblFecha);
		
		JDateChooser dateChooser = new JDateChooser();
		dateChooser.setBounds(127, 11, 87, 20);
		contentPane.add(dateChooser);
		
		JLabel horaInicio = new JLabel("Hora de inicio");
		horaInicio.setBounds(10, 42, 87, 14);
		contentPane.add(horaInicio);
		
		JLabel horaFin = new JLabel("Hora de finalizacion");
		horaFin.setBounds(10, 67, 101, 14);
		contentPane.add(horaFin);
		
		JTimeChooser timeChooser = new JTimeChooser();
		timeChooser.setBounds(127, 36, 87, 20);
		contentPane.add(timeChooser);
		
		JTimeChooser timeChooser_1 = new JTimeChooser();
		timeChooser_1.setBounds(127, 61, 87, 20);
		contentPane.add(timeChooser_1);
		
		comboBox = new JComboBox();
		comboBox.setBounds(127, 86, 87, 20);
		contentPane.add(comboBox);
		
		JLabel lblSala = new JLabel("Sala");
		lblSala.setBounds(10, 92, 46, 14);
		contentPane.add(lblSala);
		
		JButton btnCancelar = new JButton("Cancelar");
		btnCancelar.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent arg0) {
				padreClases.nullAgregarClase();
				dispose();
			}
		});
		btnCancelar.setBounds(130, 177, 89, 23);
		contentPane.add(btnCancelar);
	
		JButton btnAceptar = new JButton("Aceptar");
		btnAceptar.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent arg0) {
				if(comboBox.getSelectedIndex()!=-1)
				{
					if(timeChooser.getHours()!=0)
					{
						if(timeChooser_1.getHours()!=0)
						{
							if(dateChooser.getDate()!=null)
							{
							Time horaInicio=new Time(timeChooser.getHours(), timeChooser.getMinutes(), timeChooser.getSeconds());
							Time horaFin= new Time(timeChooser_1.getHours(), timeChooser_1.getMinutes(), timeChooser_1.getSeconds());
							java.sql.Date sqlDate = new java.sql.Date(dateChooser.getDate().getTime());
							clase= new Clase(cursada, sqlDate, horaInicio, horaFin, "Pendiente", salas.get(comboBox.getSelectedIndex()));
							ClaseControlador.guardar(clase);
							padreClases.llenarTabla();
							dateChooser.setCalendar(null);
							Time horaReseteada=new Time(0,0,0);
							timeChooser.setTime(horaReseteada);
							timeChooser_1.setTime(horaReseteada);
							comboBox.setSelectedIndex(-1);
							dispose();
							}
							else
							{
								lblError.setText("Verifique fecha");
								lblError.setVisible(true);
							}
						}
						else
						{
							lblError.setText("Verifique hora de finalizacion");
							lblError.setVisible(true);
						}
					}
					else
					{
						lblError.setText("Verifique hora de inicio");
						lblError.setVisible(true);
					}
				}
				else
				{
					lblError.setText("por favor asigne una sala");
					lblError.setVisible(true);
				}
			}
		});
		btnAceptar.setBounds(30, 177, 89, 23);
		contentPane.add(btnAceptar);
		
		lblError = new JLabel("");
		lblError.setHorizontalAlignment(SwingConstants.CENTER);
		lblError.setForeground(Color.RED);
		lblError.setBounds(10, 152, 204, 14);
		contentPane.add(lblError);
		
		JButton btnVerSalas = new JButton("Ver salas");
		btnVerSalas.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent arg0) {
				verSalas=new Salas(null,null);
				verSalas.setVisible(true);
			}
		});
		btnVerSalas.setBounds(125, 117, 89, 23);
		contentPane.add(btnVerSalas);
		this.setTitle("Agregar clase");
		lblError.setVisible(false);
		this.llenarComboSalas();
	}
}
